package edu.study.user.controller;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.common.util.cache.Lock;
import edu.study.common.util.cache.LockExecuteCallback;
import edu.study.user.api.UserApi;
import edu.study.user.api.req.AddRoleReq;
import edu.study.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserApiImpl implements UserApi {

	@Autowired
    private Lock lock;

    @Autowired
    private RoleService roleService;

    @Override
	public BaseRespDto addRole(@RequestBody AddRoleReq req) {
        BaseRespDto resp = lock.lockAndExecute(req.getUserNo() + req.getRoleName(), req.get_requestId(), new LockExecuteCallback() {
            @SuppressWarnings("unchecked")
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = roleService.addRole(req);
                return resp1;
            }
        });
		return resp;
	}
}
